package lzupdater.utils;

import lzupdater.locale.LocalizedString;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * 
 * @author Fraskor
 */
public class StatusItem {
	public enum Type {
		OK, NORMAL, ERROR;
	}

	private final static CriticalItem incorrectLoginInformation = new CriticalItem(
			"Incorrect login information.",
			"Login and/or password are invalid !");
	private final static CriticalItem fileNotFound = new CriticalItem(
			"File was missing.", "File not found !");
	private final static CriticalItem wrongUploadedFile = new CriticalItem(
			"The uploaded file was not the right one.", "Wrong uploaded file !");
	private final static CriticalItem invalidLuaFile = new CriticalItem(
			"The file does not seem to be a valid Lua file.",
			"Invalid Lua file ! Be sure to take the file 'LZProfiler.lua' in the 'World of Warcraft\\WTF\\Account\\<Your Account>\\SavedVariables\\LZProfiler.lua' directory");
	private final static CriticalItem notWellFormedFile = new CriticalItem(
			"Your file is not well formed, cannot continue.",
			"Invalid Lua file ! Be sure to take the file 'LZProfiler.lua' in the 'World of Warcraft\\WTF\\Account\\<Your Account>\\SavedVariables\\LZProfiler.lua' directory");

	private final static CriticalItem[] criticalItems = new CriticalItem[] {
			incorrectLoginInformation, fileNotFound, wrongUploadedFile,
			invalidLuaFile, notWellFormedFile };

	private Type type;
	private String msg;
	private CriticalItem criticalInfo;

	public StatusItem(String type, String msg) {
		if (type.equals("statusItemNormal")) {
			this.type = Type.NORMAL;
		} else if (type.equals("statusItemOK")) {
			this.type = Type.OK;
		} else if (type.equals("statusItemERR")) {
			this.type = Type.ERROR;
		} else {
			System.err.println("Unknown status item :" + type);
			this.type = Type.ERROR;
		}
		this.msg = msg;
		criticalInfo = null;
		for (CriticalItem ci : criticalItems) {
			if (msg.toLowerCase().contains(ci.getServeurMsg())) {
				criticalInfo = ci;
				break;
			}
		}
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isCritical() {
		return criticalInfo != null;
	}

	public LocalizedString getCriticalInfo() {
		return criticalInfo.message;
	}

	@Override
	public String toString() {
		return String.format("%-6s  : %s", type, msg);
	}

	private static class CriticalItem {
		private final String serveurMsg;
		private final LocalizedString message;

		public CriticalItem(String serveurMsg, String messageKey) {
			this.serveurMsg = serveurMsg.toLowerCase();
			message = new LocalizedString(messageKey);
		}

		public String getServeurMsg() {
			return serveurMsg;
		}

		public LocalizedString getMessage() {
			return message;
		}
	}

}
